/*
 * Created on Dec 29, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI;

/**
 * @author msoong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.awt.Insets;
import java.io.File;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import ModuleBuilder.model.*;

/* ImagePreview.java is a 1.4 example used by FileChooserDemo2.java. */
public class ImagePreview extends JComponent
						  implements PropertyChangeListener, iImagePreviewChangeListener {
						  	
	public static final int _previewIconSize=120; 
							  	
	protected ImageIcon thumbnail = null;
	protected ImageIcon fullImageIcon = null;
	ImageElement el = null;
	File file = null;
	Insets is;
	JLabel imageSizeLabel;
	
	// this field can stay null if it is used in the file chooser preview
	PictureBuilder picBuilder=null;

	public ImagePreview( JComponent parent, String frameName ) {
		// picBuilder = (PictureBuilder)parent;
		Border loweredbevel = BorderFactory.createLoweredBevelBorder();
		TitledBorder title;
		title = BorderFactory.createTitledBorder(loweredbevel, frameName);
		title.setTitlePosition(TitledBorder.ABOVE_TOP);
		setBorder(title);
		is = title.getBorderInsets(this);
		Dimension d = new Dimension(_previewIconSize+is.left+is.right, _previewIconSize+is.bottom+is.top);
		setPreferredSize(d);
		setMinimumSize(d);
		setMaximumSize(d);
		parent.addPropertyChangeListener(this);
		
	}

	public void setPictureBuilder( PictureBuilder pb ) {
		picBuilder = pb;
	}
	
	public void setImageSize(JLabel in) {
		imageSizeLabel = in;
	}
	
	public void loadImage() {
		if (file == null) {
			return;
		}
		// String fn = file.getName();
		el = ImageCacheDelegate.loadImage( file );
		if ( el != null ){
			fullImageIcon = el.getFullImageIcon();
			thumbnail = el.getSmallIcon();
		}
		// now setup the image size into the label for display
		// (this field could be null because not every instance of this 
		// have a size label
		//
		int h = fullImageIcon.getIconHeight();
		int w = fullImageIcon.getIconWidth();

		if (imageSizeLabel != null) {
			imageSizeLabel.setText(w+" pix wide x " +h+" pix high");	
		}
	}						

	public void propertyChange(PropertyChangeEvent e) {
		if ( e.getPropertyName().equals("filePath")) {
			String fn = (String)e.getNewValue();
			propertyChangeCore(fn);
		}		
	}
	
	private void propertyChangeCore( String fn ) { 	
		boolean update = true;
		file = new File( fn );
		//Update the preview accordingly.
		if (update) {
			//thumbnail = null;
			if (isShowing()) {
				loadImage();
				repaint();
			}
		}
	}	

	protected void paintComponent(Graphics g) {
		if (thumbnail == null) {
			loadImage();
		}
		if (thumbnail != null) {
			int x = getWidth()/2 - thumbnail.getIconWidth()/2;
			int y = getHeight()/2 - thumbnail.getIconHeight()/2;

			if (y < 0) {
				y = 0;
			}

			if (x < 5) {
				x = 5;
			}
			thumbnail.paintIcon(this, g, x, y+is.bottom);
		}
	}
	/**
	 * @return
	 */
	public ImageIcon getThumbnail() {
		return thumbnail;
	}

	public void imageChanged( String filePath ) {
		// There's a chance that the filePath could be null, in which case we
		// should display the default image
		if ( filePath == null ) {
			// ### need to set path to the default no figures image
			return;
		}
		propertyChangeCore( filePath );
	}
	
	public void imageChanged( ImageElement in ) {
		el = in;
		if ( el != null ) {
			thumbnail = el.getSmallIcon();
			fullImageIcon = el.getFullImageIcon();
		}
		repaint();
	}
	
	public ImageIcon getLargeImageIcon() {
		return ( fullImageIcon );
	}
	
	public ImageIcon getSmallImageIcon() {
		return ( thumbnail );
	}
	
	public void imageAdded( ImageElement el ) {
		if (picBuilder != null) {
			picBuilder.imageAdded( el );
		}
		
	}
	
	public void imageDeleted( ImageElement el ) {
		if (picBuilder != null) {
			picBuilder.imageDeleted( el );
		}
	}
	
	public ImageElement getImageElement() {
		return ( el );
	}
}

